//  Parameter details refer to:https://support.huaweicloud.com/api-eip/BatchCreatePublicips.html
resource "huaweicloud_vpc_eip" "eip" {
  count = var.eip_configuration != null ? length(var.eip_configuration) : 0
  publicip {
    type       = lookup(element(var.eip_configuration, count.index), "type")
  }

  bandwidth {
    name        = lookup(element(var.eip_configuration, count.index), "name")
    size        = lookup(element(var.eip_configuration, count.index), "size")
    share_type  = lookup(element(var.eip_configuration, count.index), "share_type")
    charge_mode = lookup(element(var.eip_configuration, count.index), "bandwidth_charge_mode")

  }
  charging_mode = var.charging_mode
  period_unit   = var.period_unit
  period        = var.period
  name          = var.name_suffix != "" ? format("%s-%s", lookup(element(var.eip_configuration, count.index), "name"), var.name_suffix) : lookup(element(var.eip_configuration, count.index), "name")
  tags = merge(
    { "Name" = var.name_suffix != "" ? format("%s-%s", lookup(element(var.eip_configuration, count.index), "name"), var.name_suffix) : lookup(element(var.eip_configuration, count.index), "name")},
    lookup(element(var.eip_configuration, count.index), "tags")
  )
}

//  Establish a public EIP binding for ELB
resource "huaweicloud_vpc_eip_associate" "associated" {
  count     = var.associated_configuration != null ? length(var.associated_configuration) : 0
  public_ip = huaweicloud_vpc_eip.eip[count.index].address
  port_id   = lookup(element(var.associated_configuration, count.index), "port_id")
}
